﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using Business;
using DevComponents.DotNetBar;
namespace Presentation
{
    public partial class frmTraCuuSach : DevComponents.DotNetBar.Office2007Form
    {
        ErrorProvider err = new ErrorProvider();
        public bool fromOtherForm = false;
        public int maSach;
        public frmTraCuuSach()
        {
            InitializeComponent();
            cmbgia.SelectedIndex = 1;
            cmbsoluong.SelectedIndex = 1;
        }

        private void frmTraCuuSach_Load(object sender, EventArgs e)
        {
 #region
            List<SachDTO> listSach = SachService.GetSachList();
            List<TheLoaiDTO> listTheLoai = TheLoaiService.GetTheLoaiList();
            List<TacGiaDTO> listTacGia = TacGiaService.GetTacGiaList();

            AutoCompleteStringCollection sach = new AutoCompleteStringCollection();
            AutoCompleteStringCollection theLoai = new AutoCompleteStringCollection();
            AutoCompleteStringCollection tacGia = new AutoCompleteStringCollection();

            foreach (SachDTO s in listSach)
            {
                sach.Add(s.TenSach);
            }
            foreach (TacGiaDTO tg in listTacGia)
            {
                tacGia.Add(tg.TenTacGia);
            }
            foreach (TheLoaiDTO tl in listTheLoai)
            {
                theLoai.Add(tl.TenTheLoai);
            }
            txtTenSach.AutoCompleteCustomSource = sach;
            txtTheLoai.AutoCompleteCustomSource = theLoai;
            txtTacGia.AutoCompleteCustomSource = tacGia;

 #endregion
            err.BlinkRate = 500;
            err.BlinkStyle = ErrorBlinkStyle.BlinkIfDifferentError;
        }

        private void btnTraCuu_Click(object sender, EventArgs e)
        {
          
            int loaiGia, loaiSoLuong;
            if (cmbgia.SelectedIndex == 1)
            {
                loaiGia = 0;
            }
            else if (cmbgia.SelectedIndex==2)
            {
                loaiGia = 1;
            }
            else
                loaiGia = -1;
            if (cmbsoluong.SelectedIndex==1)
            {
                loaiSoLuong = 0;
            }
            else if (cmbsoluong.SelectedIndex == 2)
            {
                loaiSoLuong = 1;
            }
            else
                loaiSoLuong = -1;

            List<SachDTO> listSach=new List<SachDTO>();
            if(txtSoLuong.Text=="" && txtGia.Text=="")
                listSach = SachService.Search(txtTenSach.Text, txtTheLoai.Text, txtTacGia.Text);
            else if(txtSoLuong.Text=="")
                listSach = SachService.Search(txtTenSach.Text, txtTheLoai.Text, txtTacGia.Text,double.Parse(txtGia.Text),loaiGia);
            else if(txtGia.Text=="")
                listSach = SachService.Search(txtTenSach.Text, txtTheLoai.Text, txtTacGia.Text,int.Parse(txtSoLuong.Text),loaiSoLuong);
            else
                listSach = SachService.Search(txtTenSach.Text, txtTheLoai.Text, txtTacGia.Text, double.Parse(txtGia.Text), loaiGia, int.Parse(txtSoLuong.Text), loaiSoLuong);

            DataTable dt = new DataTable();
            dt.Columns.Add("MaSach");
            dt.Columns.Add("TenSach");
            dt.Columns.Add("TheLoai");
            dt.Columns.Add("TacGia");
            dt.Columns.Add("SoLuong");
            dt.Columns.Add("DonGia");
            if (listSach.Count == 1 && listSach[0] == null)
            {
                MessageBoxEx.Show("Không tìm thấy sách nào!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            foreach (SachDTO s in listSach)
            {

                dt.Rows.Add(s.MaSach, s.TenSach, TheLoaiService.GetTheLoaiByMaLoai(s.MaTheLoai).TenTheLoai, TacGiaService.GetTacGiaByMaTacGia(s.MaTacGia).TenTacGia, s.SoLuong, s.DonGia);
            }
            dgvKetQua.DataSource = dt;
            dgvKetQua.Columns["MaSach"].HeaderText = "Mã sách";
            dgvKetQua.Columns["TenSach"].HeaderText = "Tên sách";
            dgvKetQua.Columns["TheLoai"].HeaderText = "Thể loại";
            dgvKetQua.Columns["TacGia"].HeaderText = "Tác giả";
            dgvKetQua.Columns["SoLuong"].HeaderText = "Số lượng tồn";
            dgvKetQua.Columns["DonGia"].HeaderText = "Đơn giá";
           
        }

        private void txtGia_Validating(object sender, CancelEventArgs e)
        {
            double gia=0;
            if (txtGia.Text != "" && !double.TryParse(txtGia.Text, out gia))
            {
                err.SetError(txtGia, "Nhập số");
                e.Cancel = true;
            }
            else
                err.SetError(txtGia, "");
        }

        private void txtSoLuong_Validating(object sender, CancelEventArgs e)
        {
            int sl = 0;
            if (txtSoLuong.Text != "" && !int.TryParse(txtSoLuong.Text, out sl))
            {
                err.SetError(txtSoLuong, "Nhập số");
                e.Cancel = true;
            }
            else
                err.SetError(txtSoLuong, "");
        }

        private void txtThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtHuy_Click(object sender, EventArgs e)
        {
            
            txtTenSach.Text = "";
            txtTacGia.Text = "";
            txtTheLoai.Text = "";
            txtGia.Text = "";
            txtSoLuong.Text = "";
        }

        private void dgvKetQua_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
       
        private void dgvKetQua_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (fromOtherForm && index>=0)
            {

                String str = dgvKetQua[0,index].Value.ToString();
                if (str != null)
                {
                    maSach = int.Parse(str);
                    this.Close();
                }
            }
       }

        private void labelX3_Click(object sender, EventArgs e)
        {

        }
       
    }
}
